package com.mall.malladminbackend.controller;

import com.mall.malladminbackend.dto.SpuSpueImgDto;
import com.mall.malladminbackend.entity.Spu;
import com.mall.malladminbackend.model.R;
import com.mall.malladminbackend.service.SpuService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 2022/8/17
 * 一杯茶，一根烟，一个bug解一天
 */
@RequestMapping("/spu")
@AllArgsConstructor
@RestController
public class SpuController {
    private SpuService  service;

//  查询
    @GetMapping
    public R getAllSpu(@RequestParam(defaultValue ="1") Integer page,
                       @RequestParam(defaultValue = "5") Integer size){
        return service.getAllSpe(page,size);
    }


//       添加
         @PostMapping
         public R addSpu(@RequestBody SpuSpueImgDto spuSpueImgDto){
             Spu spu = spuSpueImgDto.getSpu();
             spu.setCreateTime(new Date());
             spu.setUpdateTime(new Date());
             String[] imgList = spuSpueImgDto.getImgList();
             int[] spuKey = spuSpueImgDto.getSpuKey();
             return  service.insertSpu(spu,imgList,spuKey);
         }
//         删除
         @DeleteMapping("/{id}")
         public R deleteSpu(@PathVariable("id") Integer id){
             return  service.delete(id);
         }
//         回显
         @GetMapping("/{id}")
         public R UeditSpu(@PathVariable("id") Integer id){
            return   service.UeditSpu(id);
         }
//         编辑
       @PutMapping
      public R editSpu(@RequestBody Spu spu){
         return service.updateSpu(spu);
       }

}
